import sun.reflect.generics.tree.Tree;

import java.util.*;

public class BinaryTree {
    static class TreeNode {
        public int val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(int val) {
            this.val = val;
        }
    }

    public TreeNode TreeNode() {

        /*TreeNode A = new TreeNode('1');
        TreeNode B = new TreeNode('2');
        TreeNode C = new TreeNode('3');
        TreeNode D = new TreeNode('4');
        TreeNode E = new TreeNode('5');
        TreeNode F = new TreeNode('6');
        TreeNode G = new TreeNode('7');
        TreeNode H = new TreeNode('8');*/

        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');
        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        D.left = H;
        return A;
    }

    // 前序遍历
    public void preOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        System.out.print(root.val);
        preOrder(root.left);
        preOrder(root.right);
    }

    // 中序遍历
    public void inOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        inOrder(root.left);
        System.out.print(root.val);
        inOrder(root.right);

    }

    // 后序遍历
    public void postOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val);
    }

    public static int nodeSize;

    // 获取树中节点的个数
    public void size(TreeNode root) {
        if (root == null) {
            return;
        }
        nodeSize++;
        size(root.left);
        size(root.right);
    }

    public int size2(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int tmp = size2(root.left) + size2(root.right) + 1;
        return tmp;
    }

    // 子问题思路-求叶子结点个数 左节点的+右节点的
    // 获取叶子节点的个数
    public int getLeafNodeCount(TreeNode root) {
        if (root == null) {
            return 0;
        }
        if (root.right == null && root.left == null) {
            return 1;
        }
        int tmp = getLeafNodeCount(root.right) + getLeafNodeCount(root.left);
        return tmp;
    }

    public static int treeNode;

    public void getLeafNodeCount2(TreeNode root) {
        if (root == null) {
            return;
        }
        if (root.right == null && root.left == null) {
            treeNode++;
            return;
        }
        getLeafNodeCount2(root.left);
        getLeafNodeCount2(root.right);
    }

    // 获取第K层节点的个数
    public int getKLevelNodeCount(TreeNode root, int k) {
        if (root == null) {
            return 0;
        }
        if (k == 1) {
            return 1;
        }
        int tmp = getKLevelNodeCount(root.left, k - 1) + getKLevelNodeCount(root.right, k - 1);
        return tmp;
    }

    // 获取二叉树的高度
    public int getHeight(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int leftHeight = getHeight(root.left);
        int rightHeight = getHeight(root.right);
        return Math.max(leftHeight, rightHeight) + 1;
    }

    // 检测值为value的元素是否存在
    public TreeNode find(TreeNode root, int val) {
        if (root == null) {
            return null;
        }
        if (root.val == val) {
            return root;
        }
        TreeNode leftVal = find(root.right, val);
        if (leftVal != null) {
            return leftVal;
        }
        TreeNode rightVal = find(root.left, val);
        if (rightVal != null) {
            return rightVal;
        }
        return null;
    }

    //二叉树遍历
    //编一个程序，读入用户输入的一串先序遍历字符串，根据此字符串建立一个二叉树（以指针方式存储）。
    // 例如如下的先序遍历字符串： ABC##DE#G##F### 其中“#”表示的是空格，空格字符代表空树。
    // 建立起此二叉树以后，再对二叉树进行中序遍历，输出遍历结果。
    public static int i = 0;

    private static TreeNode createTree(String str) {
        TreeNode root = null;
        char ch = str.charAt(i);
        if (ch != '#') {
            root = new TreeNode(ch);
            i++;
            root.left = createTree(str);
            root.right = createTree(str);
        } else {
            i++;
        }
        return root;
    }

    //层序遍历
    public void levelOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()) {
            TreeNode cur = queue.poll();
            System.out.println(cur.val+" ");
            if(root.left != null) {
                queue.offer(root.left);
            }
            if(root.right != null) {
                queue.offer(root.right);
            }
        }
    }

    /*public List<List<Character>> levelOrder2(TreeNode root) {
        List<List<Character>> retList = new ArrayList<>();
        if (root == null) {
            return retList;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            int size = queue.size();
            List<Character> list = new ArrayList<>();
            while (size != 0) {
                TreeNode cur = queue.poll();
                list.add(cur.val);
                size--;
                if (cur.left != null) {
                    queue.offer(cur.left);
                }
                if (cur.right != null) {
                    queue.offer(cur.right);
                }
            }
            retList.add(list);
        }
        return retList;
    }*/
    /*public List<List<Character>> levelOrder3(TreeNode root) {
        List<List<Character>> list = new ArrayList<>();
        if(root == null){
            return null;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()){
            List<Character> list1 = new ArrayList<>();
            int i = queue.size();
            while(i != 0){
                TreeNode cur = queue.poll();
                list1.add(cur.val);
                i--;
                if(cur.left != null){
                    queue.offer(cur.left);
                }
                if(cur.right != null){
                    queue.offer(cur.right);
                }
            }
            list.add(list1);
        }
        return list;
    }*/
    public String tree2str(TreeNode root) {
        if(root == null){
            return null;
        }
        System.out.print(root.val);
        tree2str(root.left);
        tree2str(root.right);
        return null;
    }
    // 判断一棵树是不是完全二叉树
    public boolean isCompleteTree(TreeNode root){
        if(root == null){
            return true;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()) {
            TreeNode cur = queue.poll();
            if(cur != null) {
                queue.offer(cur.left);
                queue.offer(cur.right);
            }else {
                break;
            }
        }
        while(!queue.isEmpty()) {
            TreeNode list = queue.poll();
            if(list != null) {
                return false;
            }
        }
        return true;
    }



    //二叉树的最近公共祖先
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if(root == null) {
            return null;
        }
        if(root == p || root == q) {
            return root;
        }
        TreeNode left = lowestCommonAncestor(root.left,p,q);
        TreeNode right = lowestCommonAncestor(root.right,p,q);
        if(left != null && right != null) {
            return root;
        }else if(left != null) {
            return left;
        }else{
            return right;
        }
    }

    //root：根节点
    //node：想要找的指定节点
    //stack：中间存放的栈
    public boolean getPath(TreeNode root,TreeNode node,Stack<TreeNode> stack) {
        if(root == null) {
            return false;
        }
        stack.push(root);
        if(root == node) {
            return true;
        }
        boolean flgLeft = getPath(root.left,node,stack);
        if(flgLeft) {
            return true;
        }
        boolean flgLeft2 = getPath(root.right,node,stack);
        if(flgLeft2) {
            return true;
        }
        stack.pop();
        return false;
    }
    public TreeNode lowestCommonAncestor2(TreeNode root, TreeNode p, TreeNode q) {
        if(root == null) {
            return null;
        }
        if(root == p || root == q) {
            return root;
        }
        Stack<TreeNode> stack1 = new Stack<>();
        Stack<TreeNode> stack2 = new Stack<>();
        getPath(root,p,stack1);
        getPath(root,q,stack2);
        int size1 = stack1.size();
        int size2 = stack2.size();
        int size = Math.abs(size1-size2);
        if(size1 > size2) {
            while(size != 0) {
                stack1.pop();
                size--;
            }
        }else {
            while(size != 0) {
                stack2.pop();
                size--;
            }
        }
        while(!stack1.isEmpty()&&!stack2.isEmpty()) {
            if(stack1.peek().equals(stack2.peek())) {
                return stack1.pop();
            }else {
                stack1.pop();
                stack2.pop();
            }
        }
        return null;
    }

    //给定两个整数数组 preorder 和 inorder ，其中 preorder 是二叉树的先序遍历， inorder 是同一棵树的中序遍历，请构造二叉树并返回其根节点。
    /*public TreeNode buildTree(int[] preorder, int[] inorder) {
        return buildTreeChild(preorder,inorder,0,inorder.length-1);
    }
    public int preIndex;
    public TreeNode buildTreeChild(int[] preorder,int[] inorder, int inBegin, int inEnd) {
        if(inBegin > inEnd) {
            return null;
        }
        TreeNode root = new TreeNode(preorder[preIndex]);
        int rootIndex = findRootIndex(inorder,inBegin,inEnd,preorder[preIndex]);
        preIndex++;
        root.left = buildTreeChild(preorder,inorder,inBegin,rootIndex-1);
        root.right = buildTreeChild(preorder,inorder,rootIndex+1,inEnd);
        return root;
    }
    private int findRootIndex(int[] inorder,int inBegin,int inEnd,int key) {
        for(int i = inBegin; i <= inEnd; i++) {
            if(inorder[i] == key) {
                return i;
            }
        }
        return -1;
    }*/

    //给定两个整数数组 inorder 和 postorder ，其中 inorder 是二叉树的中序遍历， postorder 是同一棵树的后序遍历，请你构造并返回这颗 二叉树 。
    /*public TreeNode buildTree(int[] inorder, int[] postorder) {
        cur = inorder.length-1;
        return buildTreeChild(postorder,inorder,0,inorder.length-1);
    }
    public int cur;
    public TreeNode buildTreeChild(int[] postorderm,int[] inorder,int inBegin,int inEnd) {
        if(inBegin > inEnd) {
            return null;
        }
        TreeNode root = new TreeNode(postorderm[cur]);
        int Index = findRootIndex(postorderm[cur],inorder,inBegin,inEnd);
        cur--;
        root.right = buildTreeChild(postorderm,inorder,Index+1,inEnd);
        root.left = buildTreeChild(postorderm,inorder,inBegin,Index-1);
        return root;

    }
    public int findRootIndex(int val,int[] inorder,int inBegin,int inEnd) {
        for(int i = inBegin; inBegin <= inEnd; i++) {
            if(inorder[i] == val) {
                return i;
            }
        }
        return -1;
    }*/

    //给你二叉树的根节点 root ，请你采用前序遍历的方式，将二叉树转化为一个由括号和整数组成的字符串，返回构造出的字符串。
    //
    //空节点使用一对空括号对 "()" 表示，转化后需要省略所有不影响字符串与原始二叉树之间的一对一映射关系的空括号对。
    /*public String tree2str(TreeNode root) {
        StringBuilder sub = new StringBuilder();
        tree2strChild(root,sub);
        return sub.toString();
    }
    public void tree2strChild(TreeNode root,StringBuilder sub) {
        if(root == null) {
            return;
        }
        sub.append(root.val);
        if(root.left != null) {
            sub.append("(");
            tree2strChild(root.left,sub);
            sub.append(")");
        }else {
            if(root.right == null) {
                return;
            }else {
                sub.append("()");
            }
        }
        if(root.right != null) {
            sub.append("(");
            tree2strChild(root.right,sub);
            sub.append(")");
        }else {
            return;
        }
    }*/


//给你二叉树的根节点 root ，返回它节点值的 前序 遍历
    /*public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> list = new ArrayList<>();
        if(root == null) {
            return list;
        }
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        while (cur != null || !stack.isEmpty()) {
            while (cur != null) {
                stack.push(cur);
                list.add(cur.val);
                cur = cur.left;
            }
            TreeNode top = stack.pop();
            cur = top.right;
        }
        return list;
    }*/
    //给定一个二叉树的根节点 root ，返回 它的 中序 遍历 。

    /*public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> list = new ArrayList<>();
        if(root == null) {
            return list;
        }
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        while (cur != null || !stack.isEmpty()) {
            while (cur != null) {
                stack.push(cur);
                cur = cur.left;
            }
            TreeNode top = stack.pop();
            list.add(top.val);
            cur = top.right;
        }
        return list;
    }*/

//给你一棵二叉树的根节点 root ，返回其节点值的 后序遍历 。
    /*public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> list = new ArrayList<>();
        if(root == null) {
            return list;
        }
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        TreeNode prev = null;
        while (cur != null || !stack.isEmpty()) {
            while (cur != null) {
                stack.push(cur);
                cur = cur.left;
            }
            TreeNode top = stack.peek();
            if(top.right == null || top.right == prev) {
                stack.pop();
                list.add(top.val);
                prev = top;
            }else {
                cur = top.right;
            }
        }
        return list;
    }*/
}

